package cn.yoshii.app.todo.ui.theme

import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview

private val DarkColorScheme = darkColorScheme(
    primary = darkBlue,
    secondary = darkBlue,
    tertiary = darkBlue,
    background = black0d,
    surface = black1a,
    onPrimary = grayEd,
    onSecondary = grayEd,
    onTertiary = grayEd,
    onBackground = grayE0,
    onSurface = grayE0
)

private val LightColorScheme = lightColorScheme(
    primary = themeGreen,
    secondary = themeGreen,
    tertiary = themeGreen,
    background = grayFd,
    surface = grayEd,
    onPrimary = grayFd,
    onSecondary = grayFd,
    onTertiary = grayFd,
    onBackground = gray50,
    onSurface = gray50
)

@Composable
fun TodoTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

@Composable
fun PreviewContent(){
    Surface{
        Column {
            Text("Light Theme Text")
            Button({}){
                Text("ClickMe")
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun LightThemePreview() {
    TodoTheme(darkTheme = false, dynamicColor = false) {
        PreviewContent()
    }
}

@Preview(showBackground = true)
@Composable
fun DarkThemePreview() {
    TodoTheme(darkTheme = true, dynamicColor = false) {
        PreviewContent()
    }
}